package app.web.cartoon.analyse.tags;

import java.util.HashMap;
import java.util.Map;

import app.web.spe.CartoonSPE;
import cn.lily.com.component.schema.table.multi.TMultiCell;
import cn.lily.com.component.schema.table.multi.item.TMultiBody;
import cn.lily.com.component.schema.table.multi.item.TMultiTitle;
import cn.lily.com.db.DBFactory;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.exception.YaconException;
import cn.lily.com.lang.Basic;
import cn.lily.web.page.BusinessPage;
import cn.lily.web.widget.expand.grid.TMultiGrid;
import cn.lily.web.widget.standard.TButton;
import cn.lily.web.widget.standard.TButtonPilot;
/**
 * 动漫番数分析页面
 * @author JiaJ
 * @date 2017年12月3日
 *
 */
public class CartoonTags extends BusinessPage {
	
	/**
	 * 数据集合
	 */
	public Map<String,String> dataMap = new HashMap<String,String>();

	@Override
	public void page_onLoad() throws YaconException {
		
		TMultiGrid cartoonFanGrid = new TMultiGrid("cartoonTagsGrid");
		
		this.instGrid(cartoonFanGrid);
		
		this.instData(cartoonFanGrid);
		
		this.getPage().addElement(cartoonFanGrid);
			
	}
	

	
	/**
	 * 初始化标题
	 * @param cartoonFanGrid
	 */
	public void instGrid(TMultiGrid cartoonFanGrid){
				
		cartoonFanGrid.createView("cartoonTags", "动漫标签信息").getCell().setColSpan(5);
			
		/* 设置标题 */
		TButtonPilot cartoonFanPilot = ButtonPilot("cartoonTagsPilot");
		cartoonFanPilot.setPilotIco("/widget/buttonPilot.gif");
		cartoonFanPilot.setPilotText("动漫标签信息");
		cartoonFanPilot.addButtons(new TButton("refresh"));
		
		cartoonFanGrid.setButtonPilot(cartoonFanPilot);
		
		//设置双击事件
		cartoonFanGrid.setDataDbClick("cartoonTagsGrid_dbclick");
		
	}
	
	/**
	 * 数据加载
	 * @param testGrid
	 */
	public void instData(TMultiGrid testGrid){	

		TMultiBody body = testGrid.getBody();
		
		String sql = "select lt.TAG_NAM,lri.RES_TYP,count(*) NUM "
				   + "from ${schema}.LEO_TAG lt "
				   + "inner join ${schema}.LEO_TAG_RELA ltr on ltr.TAG_ID=lt.TAG_ID "
				   + "inner join ${schema}.LEO_RES_INFO lri on lri.RES_ID=ltr.RES_ID "
				   + "group by TAG_NAM,RES_TYP "
				   + "having RES_TYP='01' "
				   + "order by NUM DESC ";
		DataTable dt = DBFactory.getActionDB().executeQueryTable(sql);
		
		int idx = 0;
		int rowIdx = -1;
		
		while(dt.next()){
			
			if(idx % 4 == 0){
				rowIdx++;
			}
			
			body.createDataCell(rowIdx, idx % 4).setFieldValue(dt.getString("TAG_NAM")+"-"+dt.getString("NUM"));

			this.setColor(body.getDataCell(rowIdx, idx % 4), dt.getInt("NUM"));
			
			idx++;
		}
		
	}
	
	public TMultiCell setColor(TMultiCell cell,int Num){
		
		//绿色
		if(Num == 0){
			cell.setBackColor("#EAEAEA").setFontColor("#000000");
		}
		
		//中绿
		else if (Num > 0 && Num <= 10){
			cell.setBackColor("#9AFF9A").setFontColor("#000000");
		}
		
		//深绿
		else if (Num > 10 && Num <= 20){
			cell.setBackColor("#90EE90").setFontColor("#000000");
		}
		
		//蓝色
		else if (Num > 20 && Num <= 30){
			cell.setBackColor("#BBFFFF").setFontColor("#000000");
		}
		
		//蓝色
		else if (Num > 30 && Num <= 40){
			cell.setBackColor("#98F5FF").setFontColor("#000000");
		}
		
		//蓝色
		else if (Num > 40 && Num <= 50){
			cell.setBackColor("#87CEFF").setFontColor("#000000");
		}
		
		//黄色
		else if (Num > 50 && Num <= 60){
			cell.setBackColor("#FFF68F").setFontColor("#000000");
		}
		
		//黄色
		else if (Num > 60 && Num <= 70){
			cell.setBackColor("#EEEE00").setFontColor("#000000");
		}
		
		//橙色
		else if (Num > 70 && Num <= 80){
			cell.setBackColor("#FFDEAD").setFontColor("#000000");
		}
		
		//橙色
		else if (Num > 80 && Num <= 90){
			cell.setBackColor("#FFB90F").setFontColor("#000000");
		}
		
		//粉色
		else if (Num > 90 && Num <= 100){
			cell.setBackColor("#FFE1FF").setFontColor("#000000");
		}
		
		//粉色
		else if (Num > 100 && Num <= 110){
			cell.setBackColor("#FFBBFF").setFontColor("#000000");
		}
		
		//粉色
		else if (Num > 110 && Num <= 120){
			cell.setBackColor("#FF83FA").setFontColor("#000000");
		}
		
		//红色
		else if (Num > 120 && Num <= 130){
			cell.setBackColor("#FF6A6A").setFontColor("#000000");
		}
		
		//红色
		else if (Num > 130 && Num <= 150){
			cell.setBackColor("#FF00FF").setFontColor("#000000");
		}
		
		//紫色
		else {
			cell.setBackColor("#FF0000").setFontColor("#000000");
		}
		
		return cell;
		
		
	}

	/**
	 * null转换为空字符串
	 * @param str
	 * @return
	 */
	public String getString(String str){
		if(str == null){
			return "";
		}
		return str;
	}
	
	public int getNum(String fanID){
		String retNum = this.dataMap.get(fanID);
		if(retNum == null){
			retNum = "0";
		}
		return Basic.getInt(retNum);
	}

	public void refresh() throws YaconException {
		
		TMultiGrid cartoonTagsGrid = new TMultiGrid("cartoonTagsGrid");
		
		this.instGrid(cartoonTagsGrid);
		
		this.instData(cartoonTagsGrid);
			
		this.setSelfMessage(cartoonTagsGrid.getDataElement());
		
	}

}
